package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPlayBinding;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;

public class PlayActivity extends BaseAc<ActivityPlayBinding> {

    public static String videoPath;
    private long videoLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.pbPlay.setProgress(mDataBinding.videoView.getCurrentPosition()/1000);
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_play;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivPlayBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPlayStart.setOnClickListener(this);
        mDataBinding.ivPlayFull.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPlayStart:
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.bofang1);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.zanting1);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.ivPlayFull:
                PlayFullActivity.videoPath=videoPath;
                PlayFullActivity.type=0;
                startActivity(PlayFullActivity.class);
                finish();
                break;
        }
    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(videoPath)) return;
        mDataBinding.tvPlayTitle.setText(FileUtils.getFileName(videoPath));
        //设置播放器
        setPlayer();
    }

    //设置播放器
    private void setPlayer() {
        mHandler = new Handler();
        videoLength = MediaUtil.getDuration(videoPath);
        //设置seekbar
        mDataBinding.pbPlay.setMax((int) (videoLength/1000));
        //设置播放器
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.ivPlayStart.setImageResource(R.drawable.bofang1);
                mDataBinding.pbPlay.setProgress(0);
                mp.seekTo(1);
                stopTime();
            }
        });
        //开始播放视频
        mDataBinding.ivPlayStart.setImageResource(R.drawable.zanting1);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlayStart.setImageResource(R.drawable.bofang1);
        mDataBinding.videoView.pause();
        stopTime();
    }

}